#!/usr/bin/env python 
# -*- coding:utf-8 -*-

import asyncio
import os
from datetime import datetime

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from motor.motor_asyncio import AsyncIOMotorClient


def tick():
    print('Tick! The time is: %s' % datetime.now())


mongo_cof = {'host': '10.91.1.233', 'port': 27100}
connection = AsyncIOMotorClient(
    mongo_cof['host'],
    mongo_cof['port']
)

taoke_db = connection['taoke']
taoke_accounts_coll = taoke_db['taokeAccounts']


async def tick2():
    print('Tick start! The time is: %s' % datetime.now())

    taoke_accounts_coll.find({}, limit=10000)  # 查询所有文档

    # await asyncio.sleep(3)

    print('Tick end! The time is: %s' % datetime.now())
    return True


if __name__ == '__main__':
    scheduler = AsyncIOScheduler()
    # scheduler.add_job(tick, 'interval', seconds=3)
    scheduler.add_job(tick2,
                      'interval',
                      seconds=5,
                      coalesce=True,
                      misfire_grace_time=2
                      )
    scheduler.start()
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    # Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
    try:
        asyncio.get_event_loop().run_forever()
        print('aaa')
    except (KeyboardInterrupt, SystemExit):
        print('bbb')
        pass
